{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  廖之奇\n",
    "+ 学号  0162452"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我见过灵魂的伴侣\n",
      "抚摸过孤独的身体\n",
      "尝过最奢侈的爱\n",
      "才愿至死等待我爬过沙漠去看青海\n",
      "金色的油菜花正开\n",
      "风中 隐约传来阵阵圣歌\n",
      "心中 腾起解脱的欢乐\n",
      "我愿 我愿 把自己点燃\n",
      "生于最冷的冬天\n",
      "我的名字叫温暖\n",
      "\n",
      "在雪花纷飞的季节\n",
      "我盛装赶赴你的邀约\n",
      "旅途遥远或苦短\n",
      "心中都已尽欢\n",
      "我知道世界会变小\n",
      "我明白人总会变老\n",
      "往天堂 的路会越来越难走\n",
      "但囊中 有你为我备的美酒\n",
      "我愿 我愿 把自己点燃\n",
      "生于最冷的冬天\n",
      "我的名字叫温暖\n",
      "\n",
      "我见过灵魂的伴侣\n",
      "抚摸过孤独的身体\n",
      "尝过最奢侈的爱\n",
      "才愿至死等待我爬过沙漠去看青海\n",
      "金色的油菜花正开\n",
      "风中 隐约传来阵阵圣歌\n",
      "心中 腾起解脱的欢乐\n",
      "我愿 我愿 把自己点燃\n",
      "生于最冷的冬天\n",
      "我的名字叫温暖\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def chorus():\n",
    "    print(\"我见过灵魂的伴侣\\n抚摸过孤独的身体\\n尝过最奢侈的爱\\n才愿至死等待我爬过沙漠去看青海\\n金色的油菜花正开\\n风中 隐约传来阵阵圣歌\\n心中 腾起解脱的欢乐\\n我愿 我愿 把自己点燃\\n生于最冷的冬天\\n我的名字叫温暖\\n\")\n",
    "chorus()\n",
    "print(\"在雪花纷飞的季节\\n我盛装赶赴你的邀约\\n旅途遥远或苦短\\n心中都已尽欢\\n我知道世界会变小\\n我明白人总会变老\\n往天堂 的路会越来越难走\\n但囊中 有你为我备的美酒\\n我愿 我愿 把自己点燃\\n生于最冷的冬天\\n我的名字叫温暖\\n\")\n",
    "chorus()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=15\n",
      "均值=8.0\n",
      "方差=18.666666666666668\n",
      "n=11\n",
      "均值=53.90909090909091\n",
      "方差=600.082644628099\n"
     ]
    }
   ],
   "source": [
    "def count(*args):\n",
    "    x=0\n",
    "    y=0\n",
    "    n=0\n",
    "    for value in args:\n",
    "        x+=value            \n",
    "        n+=1 \n",
    "    sum=n\n",
    "    average=x/n \n",
    "    print(\"n=%s\" %n)\n",
    "    print(\"均值=%s\" %average)\n",
    "    for value in args:\n",
    "        y=y+(value-average)**2\n",
    "    print('方差=%s'%(y/sum))\n",
    "\n",
    "count(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)\n",
    "count(48,52,11,23,34,45,56,67,78,89,90)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=15\n",
      "均值=8.0\n",
      "方差=18.666666666666668\n",
      "n=11\n",
      "均值=53.90909090909091\n",
      "方差=600.082644628099\n",
      "标准差= 24.49658434615118\n"
     ]
    }
   ],
   "source": [
    "def count(*args,std=None,var=None):\n",
    "    x=0\n",
    "    y=0\n",
    "    n=0\n",
    "    for value in args:\n",
    "        x+=value            \n",
    "        n+=1 \n",
    "    sum=n\n",
    "    average=x/n \n",
    "    print(\"n=%s\" %n)\n",
    "    print(\"均值=%s\" %average)\n",
    "    for value in args:\n",
    "        y=y+(value-average)**2\n",
    "    var=y/sum\n",
    "    if var:\n",
    "        print(\"方差=%s\"%var)\n",
    "    if std:\n",
    "        import math\n",
    "        print(\"标准差=\",math.sqrt(var))\n",
    "        \n",
    "            \n",
    "count(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,var=1)\n",
    "count(48,52,11,23,34,45,56,67,78,89,90,std=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=15\n",
      "均值=8.0\n",
      "方差=18.666666666666668\n",
      "n=11\n",
      "均值=53.90909090909091\n",
      "方差=600.082644628099\n",
      "标准差= 24.49658434615118\n",
      "n=15\n",
      "均值=8.0\n",
      "方差=18.666666666666668\n",
      "标准误差= 1.1155467020454342\n",
      "n=11\n",
      "均值=53.90909090909091\n",
      "方差=600.082644628099\n",
      "最小值= 11\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def count(*args,std=None,var=None,sterror=None,maxn=None,minn=None):\n",
    "    x=0\n",
    "    y=0\n",
    "    n=0\n",
    "    for value in args:\n",
    "        x+=value            \n",
    "        n+=1 \n",
    "    sum=n\n",
    "    average=x/n \n",
    "    print(\"n=%s\" %n)\n",
    "    print(\"均值=%s\" %average)\n",
    "    for value in args:\n",
    "        y=y+(value-average)**2\n",
    "    var=y/sum\n",
    "    if var:\n",
    "        print(\"方差=%s\"%var)\n",
    "    if std:\n",
    "        print(\"标准差=\",math.sqrt(var))\n",
    "    if sterror:\n",
    "        a=math.sqrt(var)\n",
    "        b=math.sqrt(sum)\n",
    "        c=a/b\n",
    "        print(\"标准误差=\",c)\n",
    "    if maxn:\n",
    "        print(\"最大值=\",max(args))\n",
    "    if minn:\n",
    "        print(\"最小值=\",min(args))\n",
    "count(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,var=1)\n",
    "count(48,52,11,23,34,45,56,67,78,89,90,std=1)\n",
    "count(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,sterror=1)\n",
    "count(48,52,11,23,34,45,56,67,78,89,90,minn=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
